package top.techqi.core.kext

import androidx.fragment.app.Fragment

/**
 * 获取当前Fragment的所有上级Fragment列表
 *
 * @param includeSelf 是否包含自己。默认不包含
 * @param reverse 是否反转列表。默认false(从自己开始)
 */
fun Fragment.ancestors(includeSelf: Boolean = false, reverse: Boolean = false): Array<Fragment> {
    val fragments = mutableListOf<Fragment>()
    if (includeSelf) fragments.add(this)
    var fragment: Fragment? = this.parentFragment
    while (fragment != null) {
        if (reverse) {
            fragments.add(0, fragment)
        } else {
            fragments.add(fragment)
        }
        fragment = fragment.parentFragment
    }
    return fragments.toTypedArray()
}
